package com.penghaisoft.microwms.producthand.web.controller.outwarehouse;

import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProHandOutDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.product.service.IBaseDictionaryService;
import com.penghaisoft.microwms.product.service.IProHandOutService;
import com.penghaisoft.microwms.product.service.IProHandOutSnService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProHandOut;
import com.penghaisoft.microwms.common.data.model.ProHandOutSn;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/** 
* @ClassName: ProHandOutWhController 
* @Description: TODO
* @author yanggm
* @date 2018年8月24日  
*/
@RestController
@RequestMapping(value = "/pro/hand/outwh")
@Slf4j
public class ProHandOutWhController extends BaseController {
	
		@Autowired
		private IProHandOutService proHandOutService;
		
		@Autowired
		private IProHandOutSnService proHandOutSnService;
		
		@Autowired
		private IBaseDictionaryService baseDictionaryService;
		
		@Resource
		private StringRedisTemplate stringRedisTemplate;//注入对象

		/** 
		 * @Title confirm
		 * @Description 提单
		 * @copy luoteng @edit wzhg
		 * @date 2018年6月14日:上午10:15:19
		 * @param proSellOut
		 * @return HandResp
		 */
		@PostMapping("loadBill")
		public HandResp confirm(@RequestBody ProHandOut proHandOut) {
			HandResp result = new HandResp();
			Resp resp = proHandOutService.loadBill(proHandOut);
			result.setCode(resp.getCode());
			result.setMsg(resp.getMsg());
			return result;
		}
		
		/** 
		 * @Title info
		 * @Description 详情
		 * @copy luoteng @edit wzhg
		 * @date 2018年6月14日:上午10:55:29
		 * @param proSellOut
		 * @return HandResp
		 */
		@PostMapping("info")
		public HandResp info(@RequestBody ProHandOutDto proHandOutDto) {
			HandResp result = new HandResp();
			ProHandOut proHandOut = new ProHandOut();
			proHandOut.setActiveFlag("1");
			proHandOut.setHandOutNo(proHandOutDto.getHandOutNo());
//			ProHandOutSn proHandOutSn = new ProHandOutSn();
//			proHandOutSn.setHandOutNo(proHandOutDto.getHandOutNo());
//			proHandOutSn.setActiveFlag("1");
			List<ProHandOut> list = proHandOutService.queryListByStatus(proHandOut);

//			// 字典表获取型号类型的中文名
//			BaseDictionary baseDictionary1 = new BaseDictionary();
//			baseDictionary1.setActiveFlag("1");
//			baseDictionary1.setDicType(Constant.MODEL_TYPE_P);
//			List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
//
//			if(list != null && !list.isEmpty()){
//				for(ProHandOut tmpData : list){
//					if(list1 != null && !list1.isEmpty()){
//						for(BaseDictionary tmp : list1){
//							if(tmp.getDicCode().equals(tmpData.getModelType())){
//								tmpData.setModelTypeCn(tmp.getDicName());
//							}
//						}
//					}
//				}
//			}
			result.setCode(Constant.SUCCESS);
			result.setList(list);
			return result;
		}
		
		/** 
		 * @Title scan
		 * @Description 扫码
		 * @copy luoteng @edit wzhg
		 * @date 2018年6月14日:上午11:08:57
		 * @param proSellOutDto
		 * @return HandResp
		 */
		@PostMapping("scan")
		public HandResp scan(@RequestBody ProHandOutDto proHandOutDto) {
			HandResp resp = new HandResp();
			String key = Constant.PRO_SELLOUT_PREFIX + proHandOutDto.getBarcode();

			if(stringRedisTemplate.hasKey(key)){
				resp.setCode(Constant.FAIL);
				resp.setMsg("当前条码正在处理中，请不要重复扫描！");
				return resp;
			}else{
				stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

				TransResult result = new TransResult();
				try{
					result = proHandOutService.handOutScanBarcode(proHandOutDto);
					resp.setCode(result.getCode());
					resp.setMsg(result.getMsg());
					resp.setList(result.getList());
				}catch(RuntimeException e){
					resp.setCode(Constant.FAIL);
					resp.setMsg(e.getMessage());
					stringRedisTemplate.delete(key);//删除对象
				}
			}

			stringRedisTemplate.delete(key);//删除对象
			return resp;
		}
		
		/** 
		 * @Title del
		 * @Description 删除条码
		 * @copy luoteng @edit wzhg
		 * @date 2018年6月14日:下午5:10:45
		 * @param proSellOutDto
		 * @return HandResp
		 * pro/sell/outwh/scansc
		 */
		@PostMapping("scansc")
		public HandResp del(@RequestBody ProHandOutDto proHandOutDto) {
			HandResp resp = new HandResp();
			String key = Constant.PRO_SELLOUT_PREFIX + proHandOutDto.getBarcode();
			
			if(stringRedisTemplate.hasKey(key)){
				resp.setCode(Constant.FAIL);
				resp.setMsg("当前条码正在处理中，请不要重复扫描！");
				return resp;
			}else{
				stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
				
				TransResult result = new TransResult();
				try{
					result = proHandOutService.handOutDelBarcode(proHandOutDto);
					resp.setCode(result.getCode());
			        resp.setMsg(result.getMsg());
			        resp.setList(result.getList());
				}catch(RuntimeException e){
					resp.setCode(Constant.FAIL);
					resp.setMsg(e.getMessage());
					stringRedisTemplate.delete(key);//删除对象
				}
			}
			
			stringRedisTemplate.delete(key);//删除对象
			return resp;
		}
		
		/** 
		 * @Title complete
		 * @Description 确认完成
		 * @copy luoteng @edit wzhg
		 * @date 2018年6月14日:下午5:54:53
		 * @param proSellOutDto
		 * @return HandResp
		 */
		@PostMapping("complete")
		public HandResp complete(@RequestBody ProHandOutDto proHandOutDto) {
			HandResp resp = new HandResp();
			String key = Constant.PRO_SELLOUT_PREFIX + proHandOutDto.getLoginName();

			if(stringRedisTemplate.hasKey(key)){
				resp.setCode(Constant.FAIL);
				resp.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
				return resp;
			}else{
				stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间

				TransResult result = new TransResult();
				try{
					result = proHandOutService.handOutComplete(proHandOutDto);
					resp.setCode(result.getCode());
					resp.setMsg(result.getMsg());
					resp.setList(result.getList());
				}catch(RuntimeException e){
					resp.setCode(Constant.FAIL);
					resp.setMsg(e.getMessage());
					stringRedisTemplate.delete(key);//删除对象
				}
			}

			stringRedisTemplate.delete(key);//删除对象
			return resp;
		}
	  /**
	   * 新增记录
	   * @param proHandOut
	   * @return
	   */
		@PostMapping("c")
		public TransResult create(ProHandOut proHandOut) {
			proHandOut.preInsert();
			Resp resp =  proHandOutService.create(proHandOut);
			return fromResp(resp);
		}

	  /**
	   * 删除记录（逻辑删除active_flag=0)
	   * @param proHandOut
	   * @return
	   */
		@PostMapping("d")
		public TransResult delete(ProHandOut proHandOut) {
			proHandOut.preUpdate();
			Resp resp =  proHandOutService.delete(proHandOut);
			return fromResp(resp);
		}

	  /**
	   * 查询列表
	   * @param pager
	   * @return
	   */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,ProHandOut proHandOut) {
			Pager<ProHandOut> result = proHandOutService.findListByCondition(page,rows,proHandOut);
			return success(result);
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@GetMapping("r/{id}")
		public TransResult queryById(@PathVariable String id) {
			ProHandOut  proHandOut = proHandOutService.findById(id);
			return success(proHandOut);
		}

	  /**
	   * 修改记录
	   * @param proHandOut
	   * @return
	   */
		@PostMapping("u")
		public TransResult update(ProHandOut proHandOut) {
			proHandOut.preUpdate();
			Resp resp =  proHandOutService.update(proHandOut);
	    	return fromResp(resp);
		}

}
